import { Router } from '@angular/router';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Component, OnInit } from '@angular/core';
import { FormService } from "../../../core/service/tools/form.service";
import { RegisterService } from "../../../core/service/register/register.service";
import { PropupMsgReduxService } from "../../../core/service/reduxstore/msg/msg.service";
import { MsgAction } from "../../../core/service/reduxstore/msg/msg.redux";
@Component({
    styleUrls: ['./olderreg.component.css'],
    templateUrl: './olderreg.component.html'
})
export class OlderReg implements OnInit {

    formModel: FormGroup;
    files: Object;
    pwdStr:string = '';
    type:string = 'password';

    constructor(private fb: FormBuilder, private fm: FormService, private regService: RegisterService, private router: Router, private propupMsgReduxService: PropupMsgReduxService) {
        this.formModel = this.fb.group({
            userName: ['', [Validators.required], this.fm.enableUserName()],
            passwords: this.fb.group({
                password: ['', [Validators.required]],
                cpwd: ['', [Validators.required]]
            }, { validator: this.fm.ckPassword }),
            name: ['', [Validators.required]],
            id: ['', [Validators.required, this.fm.ckId]],
            selectAddress: this.fb.group({
                area: ['-1'],
                street: ['-1'],
                community: ['-1']
            }, { validator: this.fm.ckArea }),
            address: ['', [Validators.required]],
            tel: ['', [Validators.required, this.fm.ckTel]],
            smsgcode: ['', [Validators.required]],
            read: ['', [this.fm.ckRead]]
        });

    }

    upload(e: any) {
        this.files = e.srcElement.files;
    }

    doupload() {
        this.regService.uploadIdImg(this.files[0]).subscribe(res => {
            console.log(res);
            let action: MsgAction = {
                type: 'send',
                data: {
                    message: '上传成功',
                    title: '提示'
                }
            }

            this.propupMsgReduxService.dispatch(action);

            this.router.navigate(['/', { outlets: { propup: 'layer' } }]);
        });
    }

    subreg() {
        console.log(this.formModel);
        //console.log(Object.assign({}, this.formModel.value, {idimg:this.files[0]}));
    }

    ngOnInit() {
        this.formModel.get('passwords').get('password').valueChanges.debounceTime(50).subscribe(d=>{
            this.pwdStr = d;
        })
    }

    show(flg:boolean):void{
        this.type = flg ? 'text' : 'password';
    }

}